//
//  CameraView.swift
//  VPai
//
//  Created by devel on 16/9/19.
//  Copyright © 2016年 viatech. All rights reserved.
//

import UIKit
import GLKit

private let TAG = "CameraView"

class CameraView: UIView{
    
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var battery: UIImageView!
    @IBOutlet weak var indicator: UIActivityIndicatorView!

    var previewCtrl : VRPreviewController?

    override init(frame: CGRect) {

        super.init(frame: frame)//, context:context)
        initialFromXib()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        initialFromXib()
    }
    
    deinit {
        Log.info(TAG, "deinit")
    }


    
    private func initialFromXib() {
        let singleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CameraView.handleDoubleTouch(_:)))
        singleTapGestureRecognizer.numberOfTapsRequired = 1
        let doubleTapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(CameraView.handleDoubleTouch(_:)))
        doubleTapGestureRecognizer.numberOfTapsRequired = 2
        self.addGestureRecognizer(singleTapGestureRecognizer)
        self.addGestureRecognizer(doubleTapGestureRecognizer)
        //防止单击事件和双击事件冲突，只有双击事件失败时才能触发单击事件
        singleTapGestureRecognizer.requireGestureRecognizerToFail(doubleTapGestureRecognizer)
    }
    
    func createPreviewViewController(vc : UIViewController) {
        if previewCtrl != nil {
            return
        }
        previewCtrl = VRPreviewController()
        previewCtrl?.view.frame = self.bounds
        Log.debug(TAG, "previewCtrl.frame = \(self.bounds.width) x \(self.bounds.height)")
        self.insertSubview(previewCtrl!.view, atIndex: 0)
//        self.addSubview(previewCtrl!.view)

        vc.addChildViewController(previewCtrl!)
        previewCtrl!.didMoveToParentViewController(vc)
    }

    
    

    
    func handleDoubleTouch(gestureRecognizer: UITapGestureRecognizer) {
        //do nothing
    }
}
